package com.rybbaby.tss.front.tssClassesLessons.dao.imp;

import com.rybbaby.tss.core.web.dao.imp.GenericDao;
import com.rybbaby.tss.front.tssClassesLessons.dao.ITssClassesLessonsDao;
import com.rybbaby.tss.front.tssClassesLessons.entity.TssClassesLessons;
import org.springframework.stereotype.Repository;

import java.sql.Timestamp;
import java.util.List;
import java.util.Map;


/**
 * @author 雷春琼
 * @description 班级表数据库操作实现
 * @createDate 2017-08-09 下午11:50:14
 */
@Repository
public class TssClassesLessonsDaoImp extends GenericDao<TssClassesLessons> implements ITssClassesLessonsDao {




    /**
     * 获取指定ids的课节并排序
     * @param lessons
     * @param attribute
     * @param arrangement
     * @return
     */
    public List<TssClassesLessons> findListBySqlAndOrder(String lessons,String attribute,String arrangement){
        String sql = "SELECT * FROM tss_class_lessons  WHERE id IN ('"+lessons+"') ORDER BY "+attribute+" "+arrangement;
        return this.findListBySql(sql,TssClassesLessons.class,null);
    }

    @Override
    public List<Map<String, Object>> getListByClassId(String classId) {
        String hql = "SELECT *,(SELECT realName FROM sys_admin_user WHERE id = adminUserId) AS realName,(SELECT name FROM tss_class_room WHERE id=roomId) AS roomName FROM tss_class_lessons WHERE classId =? ORDER BY  classIndexTime ASC";
        return this.findForJdbc(hql, new Object[]{classId});
    }

    /**
     * 根据班级Id和亲子学生id查询剩余课时数
     *
     * @return
     */
    public Integer getSurplusCurriculum(String classId, String memberId) {
        Integer surplusClassHour = 0;
        String sql = "SELECT COUNT(1)*(SELECT expendClassHour FROM tss_classes WHERE id = ?) AS expendClassHour FROM tss_member_check_attendance WHERE classId = ? and memberId = ? AND attendenceStatus = 0 AND isDelete = 0";
        Map<String, Object> map = this.findOneForJdbc(sql, new Object[]{classId,classId,memberId});
        if (map != null && !map.isEmpty()) {
            surplusClassHour = Integer.valueOf(map.get("expendClassHour").toString());
        }
        return surplusClassHour;
    }

    @Override
    public Map<String, Object> findByClassIdCreateTime(String classId, Timestamp classTime) {
        String sql = "select * from tss_class_lessons where tss_class_lessons.createTime = ? and tss_class_lessons.classId = ?";
        return this.findOneForJdbc(sql, new Object[]{classId, classTime});
    }

    @Override
    public Map<String, Object> classLessonsExchangeByClassTime(String classTime, Integer classType) {
        String sql = "SELECT\n" +
                "\tGROUP_CONCAT(tcl.id) lessonId,\n" +
                "\ttcl.classTime,\n" +
                "\tGROUP_CONCAT(tcs. NAME) systemName\n" +
                "FROM\n" +
                "\ttss_class_lessons tcl\n" +
                "LEFT JOIN tss_classes tc ON tc.id = tcl.classId\n" +
                "LEFT JOIN tss_course tcr ON tcr.id = tc.courseId\n" +
                "LEFT JOIN sys_course_system tcs ON tcs.id = tcr.systemId\n" +
                "WHERE\n" +
                "\tclassTime =? \n" +
                "AND tc.type = ? ";
        return this.findOneForJdbc(sql, new Object[]{classTime, classType});
    }

    @Override
    public Integer updateParameterById(Map<String, Object> map, String id) {
        StringBuffer sql = new StringBuffer();
        sql.append("update tss_class_lessons set  ");
        if (map.size() > 0) {
            for (Map.Entry<String, Object> vo : map.entrySet()) {
                sql.append(vo.getKey() + "=" + "'" + vo.getValue() + "',");
            }
            sql.deleteCharAt(sql.length() - 1);
        }
        sql.append(" where id='" + id + "'");
        return this.updateBySql(sql.toString());
    }

    @Override
    public Integer countCouldChangeLessons(String id) {
        String sql = "SELECT\n" +
                "\tcount(1)\n" +
                "FROM\n" +
                "\ttss_class_lessons cl\n" +
                "LEFT JOIN tss_classes tc\n" +
                "ON tc.id=cl.classId\n" +
                "where tc.id=?\n" +
                "and cl.classTime>NOW() ";
        return this.executeSql(sql, new Object[]{id});
    }

    @Override
    public List<Map<String, Object>> queryListByClassId(String classId) {
        return null;
    }

    @Override
    public List<Map<String, Object>> queryByClassTimeAndAdminUserId(String empAdminUserId, String classTime, Integer type) {
        String sql = "select tcl.startTime,tcl.endTime,tcs.`name` from tss_class_lessons  tcl left join tss_classes tc on tc.id=tcl.classId\n" +
                "left join tss_course tco on tco.id = tc.courseId\n" +
                "left join sys_course_system tcs on tcs.id =tco.systemId\n" +
                " where tcl.adminUserId=? and  tcl.classTime=? and tc.type=?";
        return this.findForJdbc(sql, new Object[]{empAdminUserId, classTime, type});
    }
}
